package com.victor9ac.tfm.social.connector.twitter;

import javax.inject.Inject;

import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.web.context.HttpRequestResponseHolder;
import org.springframework.social.connect.Connection;
import org.springframework.social.oauth1.AuthorizedRequestToken;
import org.springframework.social.oauth1.OAuth1Operations;
import org.springframework.social.oauth1.OAuth1Parameters;
import org.springframework.social.oauth1.OAuthToken;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.social.twitter.connect.TwitterConnectionFactory;

import com.victor9ac.tfm.http.connector.HttpConnector;

public class TwitterAPIBinding {

	
	private static final String CONSUMER_SECRET = "6Sz1YjAPd3CkUDuHIG2SRHXOsLB77CmcbcZGyRJGPy0mcWLZzw";
	private static final String CONSUMER_KEY = "ZoM3RGH7x48G3KOeXsK3OTudF";
	private static final String ACCESS_TOKEN = "192599808-HPgopXHLy2TPYPC7rS83GfMlZIcYIW2ujDo5NqeG";
	private static final String ACCESS_TOKEN_SECRET = "UWq35LoK3MgaU0ziujklKkJZesIqjHHM7PIGO4HJMcgFu";
	
	@Inject
	HttpConnector httpConn;

	
	public Twitter getTwitterTemplate(){
		
		
		String consumerKey = CONSUMER_KEY; // The application's consumer key
		String consumerSecret = CONSUMER_SECRET; // The application's consumer secret
		String accessToken = ACCESS_TOKEN; // The access token granted after OAuth authorization
		String accessTokenSecret = ACCESS_TOKEN_SECRET; // The access token secret granted after OAuth authorization
		
		Twitter twitter = new TwitterTemplate(consumerKey, consumerSecret, accessToken, accessTokenSecret);
		
		
		return twitter;
	}
	
	
	public Twitter getTwitterConnection() throws Exception{
		
		TwitterConnectionFactory connectionFactory = new TwitterConnectionFactory(CONSUMER_KEY, CONSUMER_SECRET);
	
		OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations();
		
		OAuthToken requestToken = oauthOperations.fetchRequestToken("https://levanzo.com", null);
//		OAuthToken requestToken = new OAuthToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET);	
		
		
		;
		

		
	

//		response.sendRedirect(authorizeUrl);

		
		
		String url = oauthOperations.buildAuthenticateUrl(requestToken.getValue(), OAuth1Parameters.NONE);
		
		String oauthVerifier = new HttpConnector().
				sendGet(url);
		
		
		
		// upon receiving the callback from the provider:
		OAuthToken accessToken = oauthOperations.exchangeForAccessToken(
				new AuthorizedRequestToken(requestToken, "OQrGLxETh9dcGPHXddFgbiIU02CSTZwQ"), null);
		

		Connection<Twitter> connection = connectionFactory.createConnection(accessToken);
		
		return connection.getApi();
	}
}
